home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / wdj0796.zip / BENCHMAR.ZIP / NOFREE.CPP < prev   
C/C++ Source or Header  |  1996-04-26  |  2KB  |  97 lines

  1. #include <malloc.h>
  2. #if defined(__WATCOMC__)
  3. #   define _heapmin _heapshrink
  4. #endif
  5.  
  6. #include <assert.h>
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #define BIGALLOC    (16)     // in Mb
  12.  
  13. LPVOID  GetMb(int n)    {
  14.     return VirtualAlloc(0, n*1024L*1024L, MEM_RESERVE,
  15.         PAGE_READWRITE);
  16.     }
  17. void    FreeMb(LPVOID P)
  18.     {  assert(VirtualFree(P, 0, MEM_RELEASE) != FALSE);  }
  19.  
  20. // figure out how much memory is available
  21. int     AvailableMb()
  22.     {
  23.     static LPVOID   Ptrs[4096]; // I bet there isn't 4Gb
  24.     memset(Ptrs, 0, sizeof(Ptrs));
  25.  
  26.     int i;
  27.     for(i = 0; i < 4096; ++i)
  28.         {
  29.         Ptrs[i] = GetMb(1);
  30.         if(Ptrs[i] == 0)
  31.             break;
  32.         }
  33.     assert(i < 4096);
  34.     int Result  = i;
  35.  
  36.     for(i = 0; Ptrs[i]; ++i)
  37.         FreeMb(Ptrs[i]);
  38.     return Result;
  39.     }
  40.  
  41. class MemChunk
  42.     {
  43. public:
  44.     MemChunk(int Size);
  45.     static void FreeAll();
  46. private:
  47.    ~MemChunk() { if(Chunk) delete[] Chunk; }
  48.     class MemChunk*     Next;
  49.     char*               Chunk;
  50.     static MemChunk*    List;
  51.     };
  52. MemChunk* MemChunk::List = 0;
  53. MemChunk::MemChunk(int Size)
  54.     {
  55.     Chunk   = new char[Size];
  56.     assert(Chunk != 0);
  57.     Next    = List;
  58.     List    = this;
  59.     }
  60. void MemChunk::FreeAll()
  61.     {
  62.     MemChunk* Rover = List;
  63.     while(Rover)
  64.         {
  65.         Rover   = List->Next;
  66.         delete List;
  67.         List    = Rover;
  68.         }
  69.     }
  70.  
  71. void    main()
  72.     {
  73.     int     StartAmount = AvailableMb();
  74.     printf("Starting memory test (%dMb available).\n",
  75.         StartAmount);
  76.     int     CurrentAmount = StartAmount;
  77.  
  78.     // now use up at least BIGALLOC Mb of memory
  79.     while(CurrentAmount > (StartAmount - BIGALLOC))
  80.         {
  81.         for(int i = 0; i < 1024; ++i)
  82.             new MemChunk(1 + rand()%512);
  83.         CurrentAmount   = AvailableMb();
  84.         }
  85.     printf("After allocating memory, there is %dMb available\n",
  86.         CurrentAmount);
  87.     MemChunk::FreeAll();
  88.     CurrentAmount   = AvailableMb();
  89.     printf("After freeing memory, there is %dMb available\n",
  90.         CurrentAmount);
  91.     _heapmin();
  92.     CurrentAmount   = AvailableMb();
  93.     printf("After calling _heapmin(), there is %dMb available\n",
  94.         CurrentAmount);
  95.     }
  96.  
  97.